package cn.zftt.common.json;

import java.util.HashMap;
import java.util.Map;

/**
 * 返回的数据
 *
 * @author zhangfeng
 *
 */
public class Json {

    private boolean success;
    private int code;
    private String msg;
    private Map<String, Object> body;

    private Json() {
        body = new HashMap<>();
    }

    public static Json success() {
        return success(200, "成功！");
    }

    public static Json error() {
        return error(500, "出错了！");
    }

    public static Json success(int code, String msg) {
        return success(code, msg, null);
    }

    public static Json error(int code, String msg) {
        return error(code, msg, null);
    }

    public static Json success(int code, String msg, Map<String, Object> body) {
        Json json = new Json();
        json.setSuccess(true);
        json.setCode(code);
        json.setMsg(msg);
        json.setBody(body);
        return json;
    }

    public static Json error(int code, String msg, Map<String, Object> body) {
        Json json = new Json();
        json.setSuccess(false);
        json.setCode(code);
        json.setMsg(msg);
        json.setBody(body);
        return json;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public void setBody(Map<String, Object> body) {
        if (body != null)
            this.body.putAll(body);
    }

    public Json put(String key, Object value) {
        this.body.put(key, value);
        return this;
    }

    public Map<String, Object> getBody() {
        return body;
    }

}
